// questionBank/CollegeExam/CollegeExam.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 筛选相关
    showFilterModal: false,
    currentFilterType: '',
    currentFilterTitle: '',
    filterOptions: [],
    selectedSchool: '',
    selectedYear: '',
    selectedSubject: '',
    
    // 真题数据
    examGroups: [],
    hasMoreData: true,
    page: 1,
    pageSize: 10
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    // 加载初始数据
    this.loadExamData();
  },

  /**
   * 加载真题数据
   */
  loadExamData() {
    // 模拟数据，实际项目中应替换为接口请求
    const mockData = [
      {
        region: '华北地区',
        exams: [
          {
            id: 1,
            schoolName: '北京大学',
            schoolCode: 'pku',
            year: 2023,
            subject: '临床医学',
            questionCount: 65,
            hasAnswer: true,
            hot: true
          },
          {
            id: 2,
            schoolName: '清华大学',
            schoolCode: 'tsinghua',
            year: 2023,
            subject: '计算机科学与技术',
            questionCount: 58,
            hasAnswer: true,
            hot: true
          },
          {
            id: 3,
            schoolName: '南开大学',
            schoolCode: 'nankai',
            year: 2022,
            subject: '经济学',
            questionCount: 62,
            hasAnswer: true,
            hot: false
          },
          {
            id: 4,
            schoolName: '天津大学',
            schoolCode: 'tju',
            year: 2023,
            subject: '土木工程',
            questionCount: 55,
            hasAnswer: false,
            hot: false
          }
        ]
      },
      {
        region: '华东地区',
        exams: [
          {
            id: 5,
            schoolName: '复旦大学',
            schoolCode: 'fudan',
            year: 2023,
            subject: '新闻学',
            questionCount: 50,
            hasAnswer: true,
            hot: true
          },
          {
            id: 6,
            schoolName: '浙江大学',
            schoolCode: 'zju',
            year: 2023,
            subject: '计算机基础',
            questionCount: 60,
            hasAnswer: true,
            hot: true
          }
        ]
      }
    ];

    // 模拟分页加载
    const newData = this.data.page === 1 ? mockData : [];
    
    this.setData({
      examGroups: this.data.page === 1 ? newData : [...this.data.examGroups, ...newData],
      hasMoreData: newData.length >= this.data.pageSize
    });
  },

  /**
   * 显示院校筛选
   */
  showSchoolFilter() {
    const options = [
      { id: 1, name: '全部院校', value: '', selected: !this.data.selectedSchool },
      { id: 2, name: '985院校', value: '985', selected: this.data.selectedSchool === '985' },
      { id: 3, name: '211院校', value: '211', selected: this.data.selectedSchool === '211' },
      { id: 4, name: '双一流院校', value: 'doubleFirstClass', selected: this.data.selectedSchool === 'doubleFirstClass' },
      { id: 5, name: '普通本科', value: 'normal', selected: this.data.selectedSchool === 'normal' }
    ];
    
    this.setData({
      showFilterModal: true,
      currentFilterType: 'school',
      currentFilterTitle: '院校类型',
      filterOptions: options
    });
  },

  /**
   * 显示年份筛选
   */
  showYearFilter() {
    const currentYear = new Date().getFullYear();
    const options = [
      { id: 1, name: '全部年份', value: '', selected: !this.data.selectedYear }
    ];
    
    // 生成近5年选项
    for (let i = 0; i < 5; i++) {
      const year = currentYear - i;
      options.push({
        id: i + 2,
        name: `${year}年`,
        value: year.toString(),
        selected: this.data.selectedYear === year.toString()
      });
    }
    
    this.setData({
      showFilterModal: true,
      currentFilterType: 'year',
      currentFilterTitle: '考试年份',
      filterOptions: options
    });
  },

  /**
   * 显示专业筛选
   */
  showSubjectFilter() {
    const options = [
      { id: 1, name: '全部专业', value: '', selected: !this.data.selectedSubject },
      { id: 2, name: '计算机类', value: 'computer', selected: this.data.selectedSubject === 'computer' },
      { id: 3, name: '医学类', value: 'medical', selected: this.data.selectedSubject === 'medical' },
      { id: 4, name: '经济类', value: 'economic', selected: this.data.selectedSubject === 'economic' },
      { id: 5, name: '文学类', value: 'literature', selected: this.data.selectedSubject === 'literature' },
      { id: 6, name: '工学类', value: 'engineering', selected: this.data.selectedSubject === 'engineering' }
    ];
    
    this.setData({
      showFilterModal: true,
      currentFilterType: 'subject',
      currentFilterTitle: '专业分类',
      filterOptions: options
    });
  },

  /**
   * 隐藏筛选弹窗
   */
  hideFilterModal() {
    this.setData({
      showFilterModal: false
    });
  },

  /**
   * 选择筛选选项
   */
  selectFilterOption(e) {
    const value = e.currentTarget.dataset.value;
    
    switch (this.data.currentFilterType) {
      case 'school':
        this.setData({ selectedSchool: value });
        break;
      case 'year':
        this.setData({ selectedYear: value });
        break;
      case 'subject':
        this.setData({ selectedSubject: value });
        break;
    }
    
    // 重置分页，重新加载数据
    this.setData({ page: 1 });
    this.loadExamData();
    
    this.hideFilterModal();
  },

  /**
   * 加载更多
   */
  loadMore() {
    if (!this.data.hasMoreData) return;
    
    this.setData({
      page: this.data.page + 1
    });
    
    this.loadExamData();
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    this.setData({
      page: 1
    });
    
    this.loadExamData(() => {
      wx.stopPullDownRefresh();
    });
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    this.loadMore();
  }
});